<?php

namespace pmvc\mvc\controller;

use pmvc\mvc\WebFormBinder;

use pmvc\mvc\Controller;
use pmvc\mvc\Dispatcher;
use pmvc\mvc\Model;
use pmvc\mvc\Request;
use pmvc\mvc\Response;

use stdClass;

abstract class AbstractBaseCommandFormController
	extends BaseFormController {

	protected function createCommand(Request $request) {
		return new stdClass();
	}

	protected function createWebFormBinder() {
		return new WebFormBinder();
	}

	protected function getCommandName() {
		return "command";
	}

	protected function getCommandFieldNames() {
		return Array();
	}

	protected function getCommand(Response $response) {
		return $response->getModel()->get($this->getCommandName(), null);
	}

	protected function setCommand(Response $response, $command) {
		$response->getModel()->set($this->getCommandName(), $command);
	}
	
	
	
	public final function showForm(Request $request, Response $response, FormErrors $errors) {
		$command = $this->createCommand($request);
		$this->setCommand($response, $command);
		return $this->showCommandForm($command, $request, $response, $errors);
	}
	
	public function showCommandForm($command, Request $request, Response $response, FormErrors $errors) {
		$this->populateModel($request, $response, $errors);
		return $this->formView;
	}
	
	
	
	public final function validateForm(Request $request, Response $response, FormErrors $errors) {
		$command = $this->createCommand($request);
		$this->setCommand($response, $command);
		return $this->validateCommandForm($command, $request, $response, $errors);;
	}
	
	public function validateCommandForm($command, Request $request, Response $response, FormErrors $errors) {
		$binder = $this->createWebFormBinder();
		$binder->bindForm($command, $request, $errors, $this->getCommandFieldNames());
		return !$errors->hasErrors();
	}
	
	
	
	public final function showValidationError(Request $request, Response $response, FormErrors $errors) {
		return $this->showCommandValidationError($this->getCommand($response), $request, $response, $errors);
	}
	
	public function showCommandValidationError($command, Request $request, Response $response, FormErrors $errors) {
		$this->populateModel($request, $response, $errors);
		return $this->errorView;
	}
	
	
	
	public final function processForm(Request $request, Response $response, FormErrors $errors) {
		return $this->processCommandForm($this->getCommand($response), $request, $response, $errors);
	}

	public function processCommandForm($command, Request $request, Response $response, FormErrors $errors) {
		return true;
	}
	
	
	
	public final function showProcessError(Request $request, Response $response, FormErrors $errors) {
		return $this->showCommandProcessError($this->getCommand($response), $request, $response, $errors);
	}
	
	public function showCommandProcessError($command, Request $request, Response $response, FormErrors $errors) {
		$this->populateModel($request, $response, $errors);
		return $this->errorView;
	}
	
	
	
	public final function showSuccess(Request $request, Response $response, FormErrors $errors) {
		return $this->showCommandSuccess($this->getCommand($response), $request, $response, $errors);
	}
	
	public function showCommandSuccess($command, Request $request, Response $response, FormErrors $errors) {
		$this->populateModel($request, $response, $errors);
		return $this->successView;
	}

}

?>